<?php

namespace app\modules\cruise\models;

use Yii;
use yii\imagine\Image;
use Overtrue\Pinyin\Pinyin;
use yii\web\UploadedFile;

/**
 * This is the model class for table "customer".
 *
 * @property int $id
 * @property int $room_use_id 所属房间
 * @property string $first_name 姓
 * @property string $last_name 名
 * @property string $en_first_name 英文姓
 * @property string $en_last_name 英文名
 * @property string $pinyin 名字拼音
 * @property int $gender 1 男 2 女
 * @property string $birthday 出生日期
 * @property string $country 国籍
 * @property string $passport 护照号码
 * @property string $passport_add 护照签发地
 * @property string $passport_date 护照有效期
 * @property int $passport_type 护照类型 1 原件 2 复印件
 * @property string $phone 手机号
 * @property string $remark 备注说明
 * @property string $passport_grant 护照签发日期
 * @property string $card 身份证号码
 * @property string $card_address 身份证签发地
 * @property string $card_img 身份证或户口照片
 * @property string $nation 民族
 * @property int $status 1正常  2禁用
 * @property int $user_id 管理员编号
 * @property string $created_at
 * @property string $updated_at
 * @property string $group 团号
 * @property string $supply 体系
 * @property string $rank 职级
 * @property string $trip 岸上游览路线
 * @property string $size 皮肤衣尺寸
 * @property string $address 身份证上具体住址
 * @property string $career 职业
 * @property string $native 出生地
 * @property string $passport_img 护照图片
 */
class Customer extends \yii\db\ActiveRecord
{

    public $num_s;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'customer';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['room_use_id', 'room_id'], 'required'],
            [['room_use_id','room_id', 'sign', 'gender', 'passport_type', 'status', 'user_id','cancel'], 'integer'],
            [['sign_time','birthday', 'passport_date', 'passport_grant', 'created_at', 'updated_at'], 'safe'],
            [['first_name', 'last_name', 'en_first_name', 'en_last_name', 'nation'], 'string', 'max' => 32],
            [['pinyin', 'passport', 'passport_add', 'remark', 'card_img', 'passport_img'], 'string', 'max' => 255],
            [['country', 'phone', 'card_address', 'career', 'native','name','seat_area','area','ticket'], 'string', 'max' => 64],
            [['card', 'group', 'supply', 'rank', 'trip', 'size','color'], 'string', 'max' => 64],
            [['address'], 'string', 'max' => 512],
        ];
    }

        // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
         
        $pinyin = new Pinyin();    
       
        // 生成拼音 
        if(empty($this->en_first_name)){
            $this->en_first_name = strtoupper($pinyin->sentence($this->first_name));
        }

        if(empty($this->en_last_name)){
            $this->en_last_name = strtoupper($pinyin->sentence($this->last_name));
        }

        
        $name = sprintf("%s %s", $this->first_name, $this->last_name);
        $name_2 = sprintf("%s%s", $this->first_name, $this->last_name);
        $this->name = $name_2;
        $this->pinyin = $pinyin->sentence($name);
    
        return parent::beforeSave($insert);
    }//}}}


    // 保存后执行
    public function afterSave($insert, $changedAttributes){//{{{

        parent::afterSave($insert, $changedAttributes);

        if($insert){

            $orc = Customer::find()->where(['id'=>$this->id])->one();
            $or = RoomUse::findOne($orc->room_use_id);
            if($or->download == 2){
                $or->download = 1;
                $or->save();
            }

        }else{
            
            if(isset($changedAttributes)){
                $orc = Customer::find()->where(['id'=>$this->id])->one();
                $or = RoomUse::findOne($orc->room_use_id);
                if($or->download == 2){
                    $or->download = 1;
                    $or->save();
                }
            }
        }


    }//}}}

    //  获取对应房间  
    public function getRoomUse(){
        return $this->hasOne(RoomUse::className(), ['id' => 'room_use_id']);
    }

    //团号人员统计
    public function getGroupNum(){
        $group_num = ['sign'=>0,'un_sign'=>0,'cancel'=>0];
        $group_people = Customer::find()->where(['status'=>1,'group'=>$this->group])->all();
        if($group_people){
            foreach($group_people as $gp){
                if($gp->cancel < 3 && $gp->sign == 1){
                    $group_num['sign'] += 1;
                }else if($gp->cancel < 3 && $gp->sign == 0){
                    $group_num['un_sign'] += 1;
                }else if($gp->cancel == 3){
                    $group_num['cancel'] += 1;
                }
                
            }
        }
        return $group_num;
    }

     //体系人员统计
    public function getSupplyNum(){
        $supply_num = ['sign'=>0,'un_sign'=>0,'cancel'=>0];
        $supply_people = Customer::find()->where(['status'=>1,'supply'=>$this->supply])->all();
        if($supply_people){
            foreach($supply_people as $sp){
                if($sp->cancel < 3 && $sp->sign == 1){
                    $supply_num['sign'] += 1;
                }else if($sp->cancel < 3 && $sp->sign == 0){
                    $supply_num['un_sign'] += 1;
                }else if($sp->cancel == 3){
                    $supply_num['cancel'] += 1;
                }
                
            }
        }
        return $supply_num;
    }

    

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'room_use_id' => '房间ID',
            'room_id' => '房间类型',
            'sign' => '签到',
            'sign_time' => '签到时间',
            'name' => '姓名',
            'first_name' => '中文姓',
            'last_name' => '中文名',
            'en_first_name' => '英文姓',
            'en_last_name' => '英文名',
            'pinyin' => '名字拼音',
            'gender' => '性别',
            'birthday' => '出生日期',
            'country' => '国籍',
            'passport' => '护照号码',
            'passport_add' => '护照签发地',
            'passport_date' => '护照有效期',
            'passport_type' => '护照类型',
            'phone' => '手机号',
            'remark' => '备注说明',
            'passport_grant' => '护照签发日期',
            'card' => '身份证号码',
            'card_address' => '身份证签发地',
            'card_img' => '身份证或户口照片',
            'nation' => '民族',
            'status' => '状态',
            'user_id' => 'User ID',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'group' => '团号',
            'supply' => '体系',
            'rank' => '职级',
            'trip' => '岸上游览路线',
            'size' => '皮肤衣尺寸',
            'address' => '身份证上具体住址',
            'career' => '职业',
            'native' => '出生地',
            'passport_img' => '护照图片',
            'color' => '衣服颜色',
            'cancel' => '人员取消',
            'seat_area' => '座位区域',
            'area' => '大区',
            'ticket' => '培训票',
        ];
    }
}
